
Page({
  data: {
    imgList: [],
    type: 1,
    // baseUrl: 'https://total.itboye.com',
    baseUrl: 'http://127.0.0.1:7001',
    baseImgUrl: 'https://images.itboye.com/'
  },

  linkTo(){
    wx.navigateTo({
      url: '/pages/index/list',
    })
  },

  //单选选择
  radioChange(e){
    let { value } = e.detail
    this.setData({ type: value })
  },

  previewImg(e){
    let { index } = e.currentTarget.dataset
    if(this.data.imgList[index].url==""){
      wx.showToast({
        title: '等等上传完成',
      })
      return;
    }
    
    let urls = this.data.imgList.map(item=>{
      return this.data.baseImgUrl + item.url
    })

    let current = urls[index]
    wx.previewImage({
      urls,
      current
    })
  },

  //提交表单
  handleSubmit(){
    let upload = this.data.imgList.filter(item=>item.status!=1);
    if(upload.length>0){
      wx.showToast({
        title: '图片上传中..',
      })
      return;
    }
    if(this.data.imgList.length==0){
      wx.showToast({
        title: '至少上传一张',
      })
      return;
    }
    let task = this.data.imgList.map(item=>item.url)
    let that = this;
    wx.request({
      url: this.data.baseUrl+ '/api/task/create',
      method: 'POST',
      data: {
        task,
        type:this.data.type
      },
      success: r=>{
        if(r.data.code==0){
          wx.showToast({
            title: '提交成功',
          })
          that.clear();
        }else{
          wx.showToast({
            title: r.data.msg,
          })
        }
      }
    })
  },

  //清空截图
  clear(){
    this.setData({
      imgList: []
    })
  },

  //做图片压缩
  comparseImage(tempFilePath, index, len){
    let that = this;
    wx.getImageInfo({
      src: tempFilePath,
      success: function(res) {
        //尺寸超出，压缩后再上传
        if(res.height>8000){
          //做下压缩
          wx.compressImage({
            src: tempFilePath,
            compressedHeight: 8000,
            success: r=>{
              that.uploadImage(r.tempFilePath, index, len)
            },
            fail: err=>{
              console.log(err)
            }
          })
        }else{
          that.uploadImage(tempFilePath, index, len)
        }
      }
    });
  },

  //上传图片
  uploadImage(tempFilePath, index, len) {
    let that = this;
    wx.uploadFile({
      url: this.data.baseUrl + '/api/upload/single', // 你的服务器上传接口地址
      filePath: tempFilePath, // 图片临时路径
      name: 'file', // 这个字段名需要和后台商定，常见的有 'file', 'image' 等
      formData: { // 如果需要携带额外的参数，可以在这里设置
        // 'user': 'test_user',
        // 'other_data': 'some_value'
      },
      header: { // 设置请求头，例如携带Token
        'Content-Type': 'multipart/form-data'
        // 'Authorization': 'Bearer your-token-here'
      },
      success: (res) => {
        // 上传成功，res.data 中通常包含服务器返回的数据
        let result = JSON.parse(res.data);
        if(result.code==200){
          let imgList = that.data.imgList;
          imgList[index+len].url = result.data.url
          imgList[index+len].status = 1;
          that.setData({ imgList })
        }
      },
      fail: (err) => {
        // 上传失败
        wx.showToast({ title: '上传失败', icon: 'none' });
      }
    });
  },

  //点击选择相册
  handleSelect(){
    let that = this;
    wx.chooseImage({
      mediaType: ['image'],
      sourceType: ['album'],
      sizeType: ['compressed'],
      success: r=>{
        let orginImg = this.data.imgList;
        let len = orginImg.length;
        let imgList = r.tempFilePaths.map((item, index)=>{
          return { path: item, status: 0, url: '' }
        })
        orginImg = orginImg.concat(imgList)
        this.setData({imgList: orginImg})
        imgList.forEach((item,index)=>{
          that.comparseImage(item.path, index, len)
        })
      }
    })
  }
})
